package api

import (
	"github.com/astaxie/beego"
)

type RetError int

// iota 初始化后会自动递增
const (
	Failed        RetError = iota // value --> 0
	NotLogin                      // value --> 1
	ParamsInvalid                 // value --> 2
	NotFound                      // value --> 3
	NoAccess                      // value --> 4
	DatabaseError                 // value --> 5
)

type BaseController struct {
	UserId   int64
	Account  string
	Name     string
	AreaType int64
	AreaId   int64
	Identity int64
	RoleId   int64
	beego.Controller
}

type ErrorMsg struct {
	Message string `json:"message"`
	Detail  string `json:"detail"`
}

type SuccessMsg struct {
	Status string `json:"status"`
	Data   string `json:"message"`
}

var NeedTokenUriList = []string{}
var NoNeedTokenUriList = []string{}

func (this *BaseController) Success(message interface{}) {
	this.Data["json"] = map[string]interface{}{"status": 0, "data": message}
	this.ServeJSON()
	this.StopRun()
}

func (this *BaseController) Failed(err RetError, str ...string) {
	var response map[string]interface{}
	msg := &ErrorMsg{}

	if len(str) > 0 {
		msg.Detail = str[0]
	}

	switch err {
	case Failed:
		msg.Message = "操作失败"
		response = map[string]interface{}{"status": 10000, "info": msg}
	case NotLogin:
		msg.Message = "未登录授权"
		response = map[string]interface{}{"status": 10001, "info": msg}
	case ParamsInvalid:
		msg.Message = "非法参数"
		response = map[string]interface{}{"status": 10002, "info": msg}
	case NotFound:
		msg.Message = "未找到操作对象"
		response = map[string]interface{}{"status": 10003, "info": msg}
	case NoAccess:
		msg.Message = "没有操作权限"
		response = map[string]interface{}{"status": 10004, "info": msg}
	case DatabaseError:
		msg.Message = "数据库错误"
		response = map[string]interface{}{"status": 10005, "info": msg}
	}

	this.Data["json"] = response
	this.ServeJSON()
	this.StopRun()
}
